package es.uc3m.tutorial.components.general;

import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlOutputText;

import org.richfaces.component.html.HtmlColumn;
import org.richfaces.component.html.HtmlColumnGroup;
import org.richfaces.component.html.HtmlDataTable;

import es.uc3m.dei.fw2008.FacesUtils;
import es.uc3m.dei.fw2008.ui.components.DEIComponent;

public class BiblioTable extends DEIComponent {

	private String rowMouseOver;
	private String rowMouseOut;
	private String cellpadding;
	private String cellspacing;
	private String width;
	private String border;
	private String var;
	private List<String> headers;
	private String dataBeanName;
	private String dataBeanProperty;
	private List<String> dataBeanItems;
	
	public BiblioTable(){
		super();
		this.rowMouseOver = "#F1F1F1";
		this.rowMouseOut =  "#A8A8A9";
		this.cellpadding = "0";
		this.cellspacing = "0";
		this.width = "700";
		this.border = "0";
		this.var = "record";
		this.headers = null;
		this.dataBeanName = null;
		this.dataBeanProperty = null;
		this.dataBeanItems = null;
	}
	
	
	public String getRowMouseOver() {
		return rowMouseOver;
	}


	public void setRowMouseOver(String rowMouseOver) {
		this.rowMouseOver = rowMouseOver;
	}


	public String getRowMouseOut() {
		return rowMouseOut;
	}


	public void setRowMouseOut(String rowMouseOut) {
		this.rowMouseOut = rowMouseOut;
	}


	public String getCellpadding() {
		return cellpadding;
	}


	public void setCellpadding(String cellpadding) {
		this.cellpadding = cellpadding;
	}


	public String getCellspacing() {
		return cellspacing;
	}


	public void setCellspacing(String cellspacing) {
		this.cellspacing = cellspacing;
	}


	public String getWidth() {
		return width;
	}


	public void setWidth(String width) {
		this.width = width;
	}


	public String getBorder() {
		return border;
	}


	public void setBorder(String border) {
		this.border = border;
	}


	public String getVar() {
		return var;
	}


	public void setVar(String var) {
		this.var = var;
	}


	public List<String> getHeaders() {
		return headers;
	}


	public void setHeaders(List<String> headers) {
		this.headers = headers;
	}


	public String getDataBeanName() {
		return dataBeanName;
	}


	public void setDataBeanName(String dataBeanName) {
		this.dataBeanName = dataBeanName;
	}


	public String getDataBeanProperty() {
		return dataBeanProperty;
	}


	public void setDataBeanProperty(String dataBeanProperty) {
		this.dataBeanProperty = dataBeanProperty;
	}


	public List<String> getDataBeanItems() {
		return dataBeanItems;
	}


	public void setDataBeanItems(List<String> dataBeanItems) {
		this.dataBeanItems = dataBeanItems;
	}


	@Override
	public void add(DEIComponent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public DEIComponent getChild(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void remove(DEIComponent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public UIComponent paint() {
		HtmlDataTable table = new HtmlDataTable();
		table.setOnRowMouseOver(rowMouseOver);
		table.setOnRowMouseOut(rowMouseOut);
		table.setCellpadding(cellpadding);
		table.setCellspacing(cellspacing);
		table.setWidth(width);
		table.setBorder(border);
		table.setVar(var);
		table.setValue("item");

		//Se crea la tabla si los elementos no son nulos
		if(headers != null && headers.size() > 0 && dataBeanName != null && dataBeanProperty != null && dataBeanItems != null && dataBeanItems.size() != headers.size()){
			table.setValueExpression("value", FacesUtils.createValueExpression("#{"+dataBeanName+"."+dataBeanProperty+"}", List.class));

			int i = 0;
			for(String nameCol : headers){
				HtmlColumn col = new HtmlColumn();
				
				HtmlOutputText header = new HtmlOutputText();
				header.setValue(nameCol);
				
				col.getFacets().put("header", header);
				header.setParent(col);
				
				HtmlOutputText data = new HtmlOutputText();				
				data.setValueExpression("value", FacesUtils.createValueExpression("#{item."+dataBeanItems.get(i)+"}", String.class));
				col.getChildren().add(data);
				
				table.getChildren().add(col);
				i++;
			}			
		}else{
			System.err.println("Error al crea una tabla");
		}	
		return table;
	}

	

}
